package com.epfresh.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.baidu.platform.comapi.map.A;
import com.epfresh.R;
import com.epfresh.bean.HomeColumnBean;

import java.util.List;

import multitype.ItemViewBinder;

/**
 * Author: Chen
 * Date: 2020/5/26 10:25
 * Des:
 */
public class HomeColumnViewBinder extends ItemViewBinder<HomeColumnBean, HomeColumnViewBinder.ViewHolder> {
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View itemView = inflater.inflate(R.layout.item_base_spell_goods, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull final HomeColumnBean item) {
        holder.rv_goods_list.setLayoutManager(new LinearLayoutManager(holder.itemView.getContext(), RecyclerView.HORIZONTAL, false));
        holder.adapter = new ColumnProductsAdapter();
        holder.rv_goods_list.setAdapter(holder.adapter);
        holder.setProducts(item.getProducts(), item.getProductTotalCnt());
        holder.adapter.setOnColumnProductsClickListener(new ColumnProductsAdapter.OnColumnProductsClickListener() {
            @Override
            public void onColumnProductClick(HomeColumnBean.ProductsBean productsBean) {
                if (onItemClickListener != null) {
                    onItemClickListener.onColumnProductClick(productsBean);
                }
            }

            @Override
            public void showMore() {
                if (onItemClickListener != null) {
                    onItemClickListener.onMoreClick(item);
                }
            }
        });

        holder.txt_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onMoreClick(item);
                }
            }
        });

        holder.txt_title.setText(item.getName());
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView txt_title, txt_more;
        private RecyclerView rv_goods_list;
        private ColumnProductsAdapter adapter;

        ViewHolder(View itemView) {
            super(itemView);

            txt_title = itemView.findViewById(R.id.txt_title);
            txt_more = itemView.findViewById(R.id.txt_more);
            rv_goods_list = itemView.findViewById(R.id.rv_goods_list);
        }

        private void setProducts(List<HomeColumnBean.ProductsBean> productsBeans, int productsCount) {
            adapter.setProducts(productsBeans, productsCount);
        }
    }

    public interface OnItemClickListener {
        void onMoreClick(HomeColumnBean homeColumnBean);

        void onColumnProductClick(HomeColumnBean.ProductsBean productsBean);
    }
}
